package com.dwxt.community.admin.service;

import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import com.dwxt.community.dao.BroadcastHotMapper;
import com.dwxt.community.dao.BroadcastMapper;
import com.dwxt.community.entity.Broadcast;
import com.dwxt.community.entity.BroadcastHot;
import com.dwxt.community.entity.BroadcastHotExample;
import com.dwxt.community.entity.vo.BroadcastHotVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BroadcastHotService {

    @Autowired
    private BroadcastHotMapper broadcastHotMapper;
    @Autowired
    private BroadcastMapper broadcastMapper;

    public BaseResult get(Integer pageNum, Integer pageSize) {
        List<BroadcastHotVo> broadcastHotVos = broadcastHotMapper.selectBroadcastInfoByshowSortDesc();
        //用来解决 视频被删除后，热门视频表中仍然存在被删除的视频ID 问题
        List<BroadcastHotVo> invalidDatas = broadcastHotVos.stream().filter(bhVo -> ObjectUtils.isEmpty(bhVo.getBroadcast())).collect(Collectors.toList());
        invalidDatas.forEach(invalidData->{
            broadcastHotMapper.deleteByPrimaryKey(invalidData.getId());
            broadcastHotVos.removeIf(broadcastHotVo -> broadcastHotVo.getId().equals(invalidData.getId()));
        });
        return BaseResult.ok(broadcastHotVos);
    }

    public BaseResult insert(String id) {
        BroadcastHotExample broadcastHotExample = new BroadcastHotExample();
        BroadcastHotExample.Criteria criteria = broadcastHotExample.createCriteria();
        criteria.andBroadcastIdEqualTo(id);
        List<BroadcastHot> broadcastHots = broadcastHotMapper.selectByExample(broadcastHotExample);
        if (ObjectUtils.isNotEmpty(broadcastHots)) {
            return BaseResult.build(StatusCode.ALREADY_HOT);
        }
        Broadcast broadcast = broadcastMapper.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(broadcast)) {
            return BaseResult.build(StatusCode.PARAMETER_WRONG);
        }
        BroadcastHot broadcastHot = new BroadcastHot();
        broadcastHot.setBroadcastId(id);
        broadcastHot.setShowSort(broadcastHotMapper.maxShowSort() + 1);
        Date date = new Date();
        broadcastHot.setCreateTime(date);
        broadcastHot.setUpdateTime(date);
        broadcastHotMapper.insert(broadcastHot);
        return BaseResult.ok();
    }

    @Transactional(rollbackFor = Exception.class)
    public BaseResult convertor(Integer id, String type) {
        BroadcastHot broadcastHot = broadcastHotMapper.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(broadcastHot)) {
            return BaseResult.build(StatusCode.PARAMETER_WRONG);
        }
        Integer showSort = broadcastHot.getShowSort();
        if ("up".equals(type)) {
            BroadcastHot lastBroadcastHot = broadcastHotMapper.selectLastData(showSort);
            //如果上一条没有就把自己变为最后一条数据
            if (ObjectUtils.isEmpty(lastBroadcastHot)) {
                broadcastHot.setShowSort(broadcastHotMapper.minShowSort() - 1);
            } else {
                //交换位置
                broadcastHot.setShowSort(lastBroadcastHot.getShowSort());
                lastBroadcastHot.setShowSort(showSort);
            }
            broadcastHotMapper.updateByPrimaryKey(broadcastHot);
            broadcastHotMapper.updateByPrimaryKey(lastBroadcastHot);
        }
        if ("down".equals(type)) {
            BroadcastHot nextBroadcastHot = broadcastHotMapper.selectNextData(showSort);
            //如果下一条没有就把自己变为最开始的数据
            if (ObjectUtils.isEmpty(nextBroadcastHot)) {
                broadcastHot.setShowSort(broadcastHotMapper.maxShowSort() + 1);
            } else {
                //交换位置
                broadcastHot.setShowSort(nextBroadcastHot.getShowSort());
                nextBroadcastHot.setShowSort(showSort);
            }
            broadcastHotMapper.updateByPrimaryKey(broadcastHot);
            broadcastHotMapper.updateByPrimaryKey(nextBroadcastHot);
        }
        return BaseResult.ok();
    }

    public BaseResult notBroadcastHotList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Broadcast> broadcasts = broadcastMapper.notBroadcastHotList();
        return BaseResult.ok(new PageInfo<>(broadcasts));
    }

    public BaseResult del(Integer id) {
        broadcastHotMapper.deleteByPrimaryKey(id);
        return BaseResult.ok();
    }

    public int delByBroadcastId(String id) {
        BroadcastHotExample broadcastHotExample = new BroadcastHotExample();
        broadcastHotExample.createCriteria().andBroadcastIdEqualTo(id);
        return broadcastHotMapper.deleteByExample(broadcastHotExample);
    }
}
